/**     
 * @Title: UserController.java
 * @Package me.springboot.mybatis.web
 * @Description: TODO
 * @author weiwei 
 * @date 2017/08/14
 * @version V1.0     
 */
package me.springboot.mybatis.web;

import me.springboot.mybatis.core.RetResult;
import me.springboot.mybatis.core.RetResponse;
import me.springboot.mybatis.model.User;
import me.springboot.mybatis.service.UserService;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import java.util.List;
import java.util.Map;

/**
 * @ClassName: UserController
 * @Description: TODO
 * @author weiwei
 * @date 2017/08/14
 */
@RestController
@RequestMapping("/user")
public class UserController {

	@Resource
	private UserService userService;

	@PostMapping("/add")
	public RetResult<Object> add(User user) {
		userService.save(user);
		return RetResponse.makeOKRsp();
	}
	
	/**
	* @Title: add2   
	* @Description: 传入参数为JSON格式
	* @param user
	* @Reutrn RetResult<Object>
	 */
	@PostMapping("/add2")
	public RetResult<Object> add2(@RequestBody User user) {
		userService.save(user);
		return RetResponse.makeOKRsp();
	}

	@PostMapping("/delete")
	public RetResult<Object> delete(@RequestParam Integer id) {
		userService.deleteById(id);
		return RetResponse.makeOKRsp();
	}

	@PostMapping("/update")
	public RetResult<Object> update(User user) {
		userService.update(user);
		return RetResponse.makeOKRsp();
	}

	@PostMapping("/detail")
	public RetResult<User> detail(@RequestParam Integer id) {
		User user = userService.findById(id);
		return RetResponse.makeOKRsp(user);
	}
	
	/**
	* @Title: detail   
	* @Description: 传入参数为JSON格式
	* @param reqMap
	* @Reutrn RetResult<User>
	 */
	@PostMapping("/detail2")
	public RetResult<User> detail(@RequestBody Map<String, Object> reqMap) {
		Integer id = Integer.valueOf(reqMap.get("id").toString());
		User user = userService.findById(id);
		return RetResponse.makeOKRsp(user);
	}

	@PostMapping("/list")
	public RetResult<PageInfo<User>> list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
		PageHelper.startPage(page, size);
		List<User> list = userService.findAll();
		PageInfo<User> pageInfo = new PageInfo<User>(list);
		return RetResponse.makeOKRsp(pageInfo);
	}
	
	@PostMapping("/name")
	public RetResult<List<User>> name(@RequestParam String name) {
		List<User> users = userService.findListBy("userName", name);
		return RetResponse.makeOKRsp(users);
	}
}